#!/bin/bash

# Slackware build script for pli

# Written by B. Watson (urchlay@slackware.uk)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20230103 bkw: update for v0.9.10e, fix PKGTYPE mess.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=pli
VERSION=${VERSION:-0.9.10e}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

# static binary and library are for this arch.
ARCH=i586

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

# wrapper/frontend script version.
PLICLVER=${PLICLVER:-0.0.1}

set -e

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$VERSION
tar xvf $CWD/$PRGNAM-$VERSION.tgz
cd $PRGNAM-$VERSION
chown -R root:root .

# permissions are bad, please don't revert this to template.
find . -type f -a -exec chmod 644 {} \+ -o -exec chmod 755 {} \+

# binary-only software, no compilation, just install it where it goes.
# AUR has a pli build:
# https://aur.archlinux.org/cgit/aur.git/tree/PKGBUILD?h=pli-bin
# but they don't include the includes, or the ISAM stuff (libpbl),
# or the alt/ dir.

PKGLIB=$PKG/usr/lib
PRIVLIB=$PKGLIB/$PRGNAM-$VERSION
PKGDOC=$PKG/usr/doc/$PRGNAM-$VERSION
PKGMAN=$PKG/usr/man/man1
mkdir -p $PKG/usr/bin $PRIVLIB $PKGDOC $PKGMAN

install -s -oroot -groot -m0755 plic         $PRIVLIB/plic
install    -oroot -groot -m0644 lib/libprf.a $PRIVLIB/libprf.a
install    -oroot -groot -m0644 lib/libpbl.a $PRIVLIB/libpbl.a
cp -a lib/{alt,include,source} $PRIVLIB

ln -s $PRGNAM-$VERSION $PKG/usr/lib/$PRGNAM
ln -s ../lib/$PRGNAM/plic $PKG/usr/bin/plic
ln -s $PRGNAM/libprf.a $PKGLIB/libprf.a
ln -s $PRGNAM/libpbl.a $PKGLIB/libpbl.a

gzip -9c < $CWD/plic.1 > $PKGMAN/plic.1.gz

# wrapper script. written by the SlackBuild author, but not
# Slackware-specific, so it's not kept in SBo's git repo.
install -oroot -groot -m0755 $CWD/plicl-$PLICLVER $PKG/usr/bin/plicl
sh $CWD/plicl-$PLICLVER --man | gzip -9c > $PKGMAN/plicl.1.gz

# don't need 2 copies of the lgpl
rm -f lib/lgpl.html
cp -a *.gif *.pdf *.html lib/*.html samples $PKGDOC
cat $CWD/README_SBo.txt > $PKGDOC/README_SBo.txt
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
